class PositionRegistrationController {

  def index = { redirect(action:list,params:params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete:'POST', save:'POST', update:'POST']

  def list = {

    [ positionRegistrationInstanceList: PositionRegistration.list( params ), positionRegistrationInstanceTotal: PositionRegistration.count() ]
  }

  def show = {

    //
    def positionRegistrationInstance = PositionRegistration.get(params.id)

    if(!positionRegistrationInstance) {
      flash.message = "PositionRegistration not found with id ${params.id}"
      redirect(action:list)
    }
    else { return [ positionRegistrationInstance : positionRegistrationInstance ] }
  }

  def confirmation = {
    def eventInstance = Event.get( params.id )

    def positionRegistrationInstance = PositionRegistration.get( params.id )

    if(!eventInstance)
    {
      flash.message = "Event not found with id ${params.id}"
    }
    else if(!positionRegistrationInstance)
    {
      flash.message = "Position not found with id ${params.id}"
    }
    return [ eventInstance : eventInstance, positionRegistrationInstance : positionRegistrationInstance]
  }
  def whoIsSignedUp = {
    def positionInstance = Position.get( params.id )

    if(!positionInstance) {
      flash.message = "Position not found with id ${params.id}"
      redirect(action:list)
    }

    Set registrationSet = positionInstance.registrations
    List positionRegistrationInstanceList = new ArrayList(registrationSet)
    render(view: "list", model:[ positionRegistrationInstanceList: positionRegistrationInstanceList, positionRegistrationInstanceTotal: positionRegistrationInstanceList.count()])
  }

  def whoIsSignedUpForAllEventPositions = {
    Position pTemp
    def eventInstance = Event.get( params.id )

    if(!eventInstance) {
      flash.message = "Event not found with id ${params.id}"
      redirect(action:list)
    }
    Set positionSet = eventInstance.positions
    List positionList = new ArrayList(positionSet)
    if(!positionList) {
      flash.message = "Positions not found"
      redirect(action:list)
    }

    List positionRegistrationInstanceList  = new ArrayList()
    for(int i =0; i < positionList.size(); i ++){
      pTemp = positionList.get(i);
      if(pTemp.registrations){
        def pRegistrationList = new ArrayList(pTemp.registrations)
        for(int j=0; j < pTemp.registrations.size(); j++){
          PositionRegistration prObject = pRegistrationList.get(j)
          positionRegistrationInstanceList.add(prObject)
        }
      }
      pTemp = null
    }
    render(view: "list", model:[ positionRegistrationInstanceList: positionRegistrationInstanceList, positionRegistrationInstanceTotal: positionRegistrationInstanceList.count()])
  }


  def delete = {
    def positionRegistrationInstance = PositionRegistration.get( params.id )
    if(positionRegistrationInstance) {
      try {
        //update position with number of open positions
        Position p = positionRegistrationInstance.position
        int positionOpen = p.getNoOfVolunteerOpenPosition();
        p.setNoOfVolunteerOpenPosition(++positionOpen)
        p.save()

        positionRegistrationInstance.delete()
        flash.message = "PositionRegistration ${params.id} deleted"
        render("This is a confirmation that this signup request is unvolunteered!!")
        //redirect(action: whoIsSignedUpForAllEventPositions)
      }
      catch(org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "PositionRegistration ${params.id} could not be deleted"
        redirect(action:show,id:params.id)
      }
    }
    else {
      flash.message = "PositionRegistration not found with id ${params.id}"
      redirect(action:whoIsSignedUpForAllEventPositions)
    }
  }

  def edit = {
    def positionRegistrationInstance = PositionRegistration.get( params.id )

    if(!positionRegistrationInstance) {
      flash.message = "PositionRegistration not found with id ${params.id}"
      redirect(action:list)
    }
    else {
      return [ positionRegistrationInstance : positionRegistrationInstance ]
    }
  }

  def update = {
    def positionRegistrationInstance = PositionRegistration.get( params.id )
    if(positionRegistrationInstance) {
      if(params.version) {
        def version = params.version.toLong()
        if(positionRegistrationInstance.version > version) {

          positionRegistrationInstance.errors.rejectValue("version", "positionRegistration.optimistic.locking.failure", "Another user has updated this PositionRegistration while you were editing.")
          render(view:'edit',model:[positionRegistrationInstance:positionRegistrationInstance])
          return
        }
      }
      positionRegistrationInstance.properties = params
      if(!positionRegistrationInstance.hasErrors() && positionRegistrationInstance.save()) {
        flash.message = "PositionRegistration ${params.id} updated"
        redirect(action:show,id:positionRegistrationInstance.id)
      }
      else {
        render(view:'edit',model:[positionRegistrationInstance:positionRegistrationInstance])
      }
    }
    else {
      flash.message = "PositionRegistration not found with id ${params.id}"
      redirect(action:edit,id:params.id)
    }
  }

  def create = {
    def positions
    if (params.eventId){
    positions = Position.findByEvent(Event.get(params.eventId))

    }
    def positionRegistrationInstance = new PositionRegistration()
    positionRegistrationInstance.properties = params
    if(positions == null){
      flash.message = "No positions found for this Event"
    }
    return ['positionRegistrationInstance':positionRegistrationInstance, positions:positions]
  }

  def save = {
    PositionRegistration positionRegistrationInstance = new PositionRegistration(params)
    UserAccount userSession = session.getAttribute("userAccount")
    UserAccount refreshedUser = UserAccount.get(userSession.id)
    positionRegistrationInstance.setUser(refreshedUser)
    
    Position p = positionRegistrationInstance.position
    int positionOpen = p.getNoOfVolunteerOpenPosition();
    if(positionOpen > 0 ){
      p.setNoOfVolunteerOpenPosition(--positionOpen) 
      p.save()
    }else{
      render("This position does not have open positions, Sorry!!")
      return
    }
    if(!positionRegistrationInstance.hasErrors() && positionRegistrationInstance.save()) {
      flash.message = "PositionRegistration ${positionRegistrationInstance.id} created"
      //redirect(action:"confirmationpage",id:positionRegistrationInstance.id)
      render(view:'confirmationpage',model:[positionRegistrationInstance:positionRegistrationInstance])
      return
    }
    else {
      render(view:'create',model:[positionRegistrationInstance:positionRegistrationInstance])
    }
  }
}
